package olga.gorbatiuk.journal.impl;

import static org.junit.Assert.*;
import olga.gorbatiuk.record.Record;

import org.junit.Test;

public class ArrayJournalTest {

    @Test
    public void add() {
	String message = "2014-11-10 20:30:40 ONE источник_сообщение";
	Record record = new Record(message);

	ArrayJournal arrayJournal = new ArrayJournal();

	assertEquals(0, arrayJournal.size());
	arrayJournal.add(record);
	assertEquals(1, arrayJournal.size());
    }

    @Test
    public void addAll() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	arrayJournal.add(record1);
	arrayJournal.add(record2);

	ArrayJournal actualArrayJournal = new ArrayJournal();
	assertEquals(0, actualArrayJournal.size());

	actualArrayJournal.add(arrayJournal);

	assertEquals(2, actualArrayJournal.size());
	assertEquals(record2, arrayJournal.get(1));
	assertEquals(record1, arrayJournal.get(0));
    }

    @Test
    public void remove() {
	String message = "2014-11-10 20:30:40 ONE источник_сообщение";
	Record record = new Record(message);

	ArrayJournal arrayJournal = new ArrayJournal();

	assertEquals(0, arrayJournal.size());
	arrayJournal.add(record);
	arrayJournal.add(record);
	assertEquals(2, arrayJournal.size());
	arrayJournal.remove(record);
	assertEquals(1, arrayJournal.size());
    }

    @Test
    public void get() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);

	ArrayJournal arrayJournal = new ArrayJournal();

	assertEquals(0, arrayJournal.size());
	arrayJournal.add(record1);
	arrayJournal.add(record2);
	assertEquals(record2, arrayJournal.get(1));
	assertEquals(record1, arrayJournal.get(0));
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void set() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	assertEquals(0, arrayJournal.size());
	arrayJournal.add(record1);
	arrayJournal.add(record1);
	arrayJournal.add(record1);
	arrayJournal.set(1, record2);
	assertEquals(record2, arrayJournal.get(1));
	assertEquals(record1, arrayJournal.get(0));
	arrayJournal.set(4, record2);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void insert() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	assertEquals(0, arrayJournal.size());
	arrayJournal.add(record1);
	arrayJournal.add(record1);
	arrayJournal.add(record1);
	arrayJournal.insert(1, record2);
	arrayJournal.insert(0, record2);
	assertEquals(record2, arrayJournal.get(2));
	assertEquals(record2, arrayJournal.get(0));
	arrayJournal.insert(6, record2);
    }

    @Test
    public void removeByIndex() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	assertEquals(0, arrayJournal.size());
	arrayJournal.add(record1);
	arrayJournal.add(record2);
	assertEquals(2, arrayJournal.size());
	arrayJournal.remove(1);
	assertEquals(1, arrayJournal.size());
    }

    public void removeRange() {
	fail("not supported yet");
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void removeAll() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	assertEquals(0, arrayJournal.size());
	arrayJournal.add(record1);
	arrayJournal.add(record2);
	assertEquals(2, arrayJournal.size());
	arrayJournal.removeAll();
	assertEquals(0, arrayJournal.size());
	arrayJournal.get(1);
    }

    @Test
    public void filterString() {
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	arrayJournal.add(record0);
	arrayJournal.add(record1);
	arrayJournal.add(record2);

	ArrayJournal expectedArrayJournal = new ArrayJournal();
	arrayJournal.add(record0);
	arrayJournal.add(record1);

	assertEquals(expectedArrayJournal, arrayJournal.filter(message0));
    }

    public void filterDate() {
	fail("not supported yet");
    }

    @Test
    public void sortByDate() {
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-06 12:30:40 ONE источник_сообщение";
	String message2 = "2014-11-20 22:35:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	arrayJournal.add(record0);
	arrayJournal.add(record1);
	arrayJournal.add(record2);

	arrayJournal.sortByDate();
	assertEquals(record1, arrayJournal.get(0));
	assertEquals(record0, arrayJournal.get(1));
	assertEquals(record2, arrayJournal.get(2));
    }

    @Test
    public void sortByImportanceDate() {
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-06 12:30:40 THREE источник_сообщение";
	String message2 = "2014-11-20 22:35:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	ArrayJournal arrayJournal = new ArrayJournal();
	arrayJournal.add(record0);
	arrayJournal.add(record1);
	arrayJournal.add(record2);

	arrayJournal.sortByImportanceDate();
	assertEquals(record0, arrayJournal.get(0));
	assertEquals(record2, arrayJournal.get(1));
	assertEquals(record1, arrayJournal.get(2));
    }

    public void sortByImportanceSourceDate() {
	fail("not supported yet");
    }

    public void sortBySourceDate() {
	fail("not supported yet");
    }
}
